package com.infindo.iflower;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.infindo.common.Constant;
import com.infindo.iflower.util.AssetsUtil;
import com.infindo.iflower.util.FileUtil;

public class SendMethod extends Activity {
	private Intent intent;
	private Bundle bundle;
	private int picindex;

	private ImageView image;
	//private String[] picNames;
	private Activity mActivity;
	private String prebtntype;
	private String picName;
	private String[] picNames;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.picsend_dialog);
		mActivity = this;
		//picNames = AssetsUtil.getPictureList(this);

		// get paramters
		intent = this.getIntent();
		bundle = intent.getExtras();
		picindex = bundle.getInt("picIndex");
		prebtntype = bundle.getString(Constant.ClASS_METHOD); //if favorite button
		picName = bundle.getString("picName"); //if favorite button
		
		System.out.println(".........picname = ..."+picName);
		picNames = bundle.getStringArray("picNames");
		final WallpaperManager wallpaperManager = WallpaperManager
				.getInstance(this);
//		final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
//		final WallpaperManager wallpaperManager = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
		

		
		image = (ImageView) findViewById(R.id.wallpic);
		image.setDrawingCacheEnabled(true);

		// share to friends
		Button emailBtn = (Button) findViewById(R.id.sharetofriend);
		emailBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(SendMethod.this,EmailWrite.class);
				Bundle bundle = new Bundle();
				bundle.putInt("picIndex", picindex);
				bundle.putString("clazz", "SendMethod_share");
				bundle.putStringArray("picNames", picNames);
				bundle.putString(Constant.ClASS_METHOD, prebtntype);
				i.putExtras(bundle);
				startActivity(i);

			}
		});

		// add favorite
		Button addfavoriteBtn = (Button) findViewById(R.id.addfavorite);
		if(Constant.FILENAME_FROM_FAVORITE.equals(prebtntype)){
			addfavoriteBtn.setText(R.string.deltofavorite);
		}
		addfavoriteBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(Constant.FILENAME_FROM_FAVORITE.equals(prebtntype)){
					delfromSD(picName);
					setResult(RESULT_OK);
				}
				else{
					addtoSD();
				}
				Intent t = new Intent(mActivity,GridFavorite.class);
				startActivity(t);
				finish();
			}
		});


		// save photo
		Button savephotoBtn = (Button) findViewById(R.id.savephoto);
		savephotoBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				//String filename = picNames[picindex];// default save to data/data/com.infindo.iflower
				savetoLocal(picName);
				finish();
			}
		});

		// user as wallpaper
		Button useWallpaperBtn = (Button) findViewById(R.id.usewallpaper);
		useWallpaperBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				Intent intent = new Intent();   
//				intent.setType("image/*");   
//				intent.setAction(Intent.ACTION_GET_CONTENT);   
//				startActivityForResult(Intent.createChooser(intent, "Select Picture"),1); 
////				startActivity(Intent.createChooser(intent, "Select Picture")); 

				try {
//					Uri.withAppendedPath(contactRef,ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
					
//					wallpaperManager.suggestDesiredDimensions(960, 800);
					
//					wallpaperManager.setWallpaperOffsetSteps(1, 1);
					if(Constant.FILENAME_FROM_FAVORITE.equals(prebtntype)){ 
						InputStream in = new FileInputStream(picName);
						wallpaperManager.setStream(in);
				
					} else {
						wallpaperManager.setStream(AssetsUtil.getPictureStream(mActivity, AssetsUtil.PICTURE_PATH_BIG + "/", picName));

					}

					Toast.makeText(mActivity, R.string.setwallpapersuccess,
							Toast.LENGTH_SHORT).show();
				} catch (IOException e) {
					Toast.makeText(mActivity, R.string.setwallpaperfail,
							Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				mActivity.finish();
			}
		});

		// back to previous page
		Button cancelBtn = (Button) findViewById(R.id.sendcancel);
		cancelBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	
	private void delfromSD(String filename) {
		try {
			FileUtil.delFile(filename);
			Toast.makeText(mActivity, R.string.fav_del_success,
					Toast.LENGTH_LONG);
		} catch (IOException e1) {
			Toast.makeText(mActivity, R.string.fav_del_failure,
					Toast.LENGTH_SHORT);
		}

	}
	
	
	private void addtoSD(){
		if (FileUtil.isSdPresent()) {
			if (AssetsUtil.saveAssetsToSdcard(mActivity, AssetsUtil.PICTURE_PATH_BIG + "/",
					picName, Constant.FAV_PATH)) {
				// save success, show success message
				Toast.makeText(mActivity, R.string.fav_add_success,
						Toast.LENGTH_SHORT).show();
			} else {
				// failure
				Toast.makeText(mActivity, R.string.fav_add_failure,
						Toast.LENGTH_SHORT).show();
			}

		} else {
			Toast.makeText(mActivity, R.string.fav_no_sdcard,
					Toast.LENGTH_SHORT).show();
		}
	}
	
	private void savetoLocal(String filename){
		try {
			String fileNameWithoutPath = filename;
			InputStream in = null;
			if(Constant.FILENAME_FROM_FAVORITE.equals(prebtntype)){ 
				in = new FileInputStream(filename);
				fileNameWithoutPath = fileNameWithoutPath.substring(fileNameWithoutPath.lastIndexOf(File.separator) + 1);
				
			} else {
				in = AssetsUtil.getPictureStream(mActivity, AssetsUtil.PICTURE_PATH_BIG + "/", filename);
			}
			FileOutputStream out = mActivity.openFileOutput(fileNameWithoutPath,
					Context.MODE_WORLD_WRITEABLE);
			
			int read = 0;
			while (in != null && (read = in.read()) != -1) {
				out.write((byte) read);
			}
			// close stream
			if (in != null)
				in.close();
			if (out != null) {
				out.flush();
				out.close();
			}
			Toast.makeText(mActivity, R.string.photo_save_success,
					Toast.LENGTH_SHORT).show();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			Toast.makeText(mActivity, R.string.file_not_found,
					Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			e.printStackTrace();
			Toast.makeText(mActivity, R.string.file_not_found,
					Toast.LENGTH_SHORT).show();
		}
	}
}
